import { HttpError, toSuccess, useEntityManager } from "@sker/core"
import { KsAccount, KsAccountLog } from "@sker/entities"
import { useData } from "@sker/runtime"
import dayjs from "dayjs"
import { z } from "zod"

export default async () => {
    const input = z.object({
        user_id: z.string(),
        living: z.boolean()
    })
    const body = input.parse(useData())
    await useEntityManager(async m => {
        const account = await m.findOne(KsAccount, { where: { user_id: body.user_id } })
        if (!account) throw new HttpError(`参数错误`)
        const log = m.create(KsAccountLog, { account_id: account.id, time: dayjs().format('HH:mm:ss'), living: body.living })
        await m.save(KsAccountLog, log)
        await m.update(KsAccount, account.id, { update_date: new Date(), has_live: body.living })
    })
    return toSuccess(true)
}
